package org.tao.lightningmq.broker.timewheel;

/**
 * @Author lht
 * @date 2025/1/22 - 14:07
 * @description:
 */
public class TimeWheelModel {
    private int current;
    private TimeWheelSlotListModel[] timeWheelSlotListModel;
    /**
     * 时间轮的存储时间单位
     *
     * @see TimeWheelSlotStepUnitEnum
     */
    private String unit;

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public TimeWheelSlotListModel[] getTimeWheelSlotListModel() {
        return timeWheelSlotListModel;
    }

    public void setTimeWheelSlotListModel(TimeWheelSlotListModel[] timeWheelSlotListModel) {
        this.timeWheelSlotListModel = timeWheelSlotListModel;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public int countNextSlot(int delay) {
        if (delay >= timeWheelSlotListModel.length) {
            throw new RuntimeException("delay can not large than slot's total count");
        }
        int remainSlotCount = timeWheelSlotListModel.length - current;
        int diff = delay - remainSlotCount;
        if (diff < 0) {
            return current + delay;
        }
        return diff;
    }
}
